const fetch = require('node-fetch')

// 主页 api
const URL_HOME = 
    "https://bbs-api.mihoyo.com/apihub/wapi/webHome?gids={gids}&page=1&page_size=20"
const URL_HOME_NEW = 
    "https://bbs-api.mihoyo.com/apihub/api/home/new?gids={gids}"
// 静态 表情包 
const URL_EMOTICON =
    "https://bbs-api-static.mihoyo.com/misc/wapi/emoticon_set?gids={gids}"
// 主页 文章点赞等数据
const URL_DYNAMICDATA =
    "https://bbs-api.mihoyo.com/post/wapi/getDynamicData?gids={gids}&post_ids={post_ids}"
// 搜索页 全部tab
const URL_SEARCH_DEFAULT = 
    "https://bbs-api.mihoyo.com/post/wapi/searchPosts?gids={gids}&keyword={keyword}&size=20&type={type}"


const fetchHomeByPatition = (gids) => {
    return fetch(URL_HOME.replace("{gids}", gids))
        .then(res => res.json())
        .then(json=>json)
}

const fetchHomeNewByPatition = (gids) => {
    return fetch(URL_HOME_NEW.replace("{gids}", gids))
        .then(res => res.json())
        .then(json => json)
}

const fetchEmoticonByPatition = (gids) => {
    return fetch(URL_EMOTICON.replace("{gids}", gids))
        .then(res => res.json())
        .then(json => json)
}

const fetchDynamicDataByPostId = (gids, post_ids) => {
    return fetch(URL_DYNAMICDATA.replace("{gids}", gids).replace("{post_ids}", post_ids))
        .then(res => res.json())
        .then(json => json)
}

const fetchSearchData = (gids, keyword, type) => {
    return fetch(
        encodeURI(
        URL_SEARCH_DEFAULT
            .replace("{gids}", gids)
            .replace("{keyword}", keyword)
            .replace("{type}", type)
        )
    )
        .then(res => res.json())
        .then(json => json)
}

module.exports = {
    fetchHomeByPatition,
    fetchHomeNewByPatition,
    fetchEmoticonByPatition,
    fetchDynamicDataByPostId,
    fetchSearchData,
}